﻿using System;
using System.Collections.Generic;
using Dexter.PluginManager;
using Dexter.Plugins.SelectiveTwitter.Binder;
using Dexter.Web.Mvc.Controllers;

namespace Dexter.Plugins.SelectiveTwitter {
	public class PluginConfiguration : IPluginConfiguration {
		public object GetBinder(string serializedConfiguration)
		{
			Configuration conf = ConfigurationHelper.GetConfiguration;

			if (!string.IsNullOrEmpty(serializedConfiguration))
				conf = serializedConfiguration.Deserialize<Configuration>();

			SelectiveTwitterConfigurationBinder binder = new SelectiveTwitterConfigurationBinder();

			binder.TwitterName = conf.Owner;
			binder.PageTitle = conf.PageTitle;
			binder.PageSlug = conf.PageSlug;
			binder.SubPageTitleFormat = conf.SubPageTitleFormat;
			binder.SubPageSlugFormat = conf.SubPageSlugFormat;
			binder.FetchMinutesFrequency = 10;
			binder.GrabMentions = string.Join(",", conf.GrabCriteria.Mentions);
			binder.GrabHashTags = string.Join(",", conf.GrabCriteria.HashTags);
			binder.CategoryName1 = conf.Categories[0].CategoryName;
			binder.CategoryName2 = conf.Categories[1].CategoryName;
			binder.CategoryName3 = conf.Categories[2].CategoryName;

			binder.HashTags1 = string.Join(",", conf.Categories[0].HashTags);
			binder.HashTags2 = string.Join(",", conf.Categories[1].HashTags);
			binder.HashTags3 = string.Join(",", conf.Categories[2].HashTags);

			return binder;
		}

		public string SerializeConfiguration(object configuration) {
			var binder = configuration as SelectiveTwitterConfigurationBinder;

			if(binder == null)
				return ConfigurationHelper.GetConfiguration.Serialize();

			Configuration conf = new Configuration();
			conf.Owner = binder.TwitterName;
			conf.PageTitle = binder.PageTitle;
			conf.PageSlug = binder.PageSlug;
			conf.SubPageTitleFormat = binder.SubPageTitleFormat;
			conf.SubPageSlugFormat = binder.SubPageSlugFormat;
			conf.FetchMinutesFrequency = 10;

			conf.GrabCriteria = new GrabCriteria();
			conf.GrabCriteria.Mentions = binder.GrabMentions.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
			conf.GrabCriteria.HashTags = binder.GrabHashTags.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

			conf.Categories = new List<Hashtag>();

			if (!string.IsNullOrEmpty(binder.CategoryName1) && !string.IsNullOrEmpty(binder.HashTags1)) {
				Hashtag ht = new Hashtag();
				ht.CategoryName = binder.CategoryName1;
				ht.HashTags = binder.HashTags1.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

				conf.Categories.Add(ht);
			}

			if (!string.IsNullOrEmpty(binder.CategoryName2) && !string.IsNullOrEmpty(binder.HashTags2))
			{
				Hashtag ht = new Hashtag();
				ht.CategoryName = binder.CategoryName2;
				ht.HashTags = binder.HashTags2.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

				conf.Categories.Add(ht);
			}

			if (!string.IsNullOrEmpty(binder.CategoryName3) && !string.IsNullOrEmpty(binder.HashTags3))
			{
				Hashtag ht = new Hashtag();
				ht.CategoryName = binder.CategoryName3;
				ht.HashTags = binder.HashTags3.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

				conf.Categories.Add(ht);
			}

			if (!string.IsNullOrEmpty(binder.CategoryName4) && !string.IsNullOrEmpty(binder.HashTags4))
			{
				Hashtag ht = new Hashtag();
				ht.CategoryName = binder.CategoryName4;
				ht.HashTags = binder.HashTags4.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

				conf.Categories.Add(ht);
			}

			return conf.Serialize();
		}

		public bool HasConfiguration {
			get { return true; }
		}

		public void ValidateBinder(BackOfficeControllerBase controller) {}
	}
}